<?php
/**
 * Created by PhpStorm.
 * User: 9ey
 * Date: 2019/4/26
 * Time: 9:23
 */

namespace App\Utils;

use App\Exceptions\SysException;

/**
 * 验证
 * Class Verify
 * @package App\Utils
 */
class Verify
{
    static function int($value)
    {
        return filter_var($value, FILTER_VALIDATE_INT);
    }

    static function money($value, $total = 10, $places = 2)
    {
        if (empty($value)) {
            return 0;
        }
        $total = $total - $places;
        if (!preg_match('/(^-?[1-9](\d{0,' . ($total - 1) . '})?(\.\d{1,' . ($places) . '})?$)|(^(0)$)|(^-?\d\.\d{1,' . ($places) . '}?$)/', $value)) {
            return false;
        }
        return floatval($value);
    }

    static function intOrFail($value, $msg = '数据格式错误')
    {
        if (($value = self::int($value)) === false) {
            throw new SysException($msg);
        }
        return $value;
    }

    static function moneyOrFail($value, $msg = '金额格式错误')
    {
        if (($value = self::money($value)) === false) {
            throw new SysException($msg);
        }
        return $value;
    }


}
